package cn.sumpu.app.wlcity.biz.impl.sms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.wlcity.biz.SmsBiz;
import cn.sumpu.app.wlcity.dao.SmsLogDao;
import cn.sumpu.app.wlcity.domain.SmsLog;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.IdGenerator;

public class SmsBizImpl implements SmsBiz {

	private static Logger logger = LoggerFactory.getLogger(SmsBizImpl.class);

	private SmsLogDao smsLogDao;

	public void setSmsLogDao(SmsLogDao smsLogDao) {
		this.smsLogDao = smsLogDao;
	}

	@Override
	public String send(String phoneNumber, String content)
			throws AppBusinessException {
		if (null == phoneNumber || null == content) {
			if (logger.isErrorEnabled()) {
				logger.error("Send sms failure,phoneNumber is null || content is null.");
			}
			throw new AppBusinessException(
					"Send sms failure,phoneNumber is null || content is null.");
		}
		try {
			MessageService server = new MessageService();
			MessageServerPortType op = server.getMessageServerPort();
			String result = op.send(phoneNumber, content);

			SmsLog sms = new SmsLog();
			sms.setIdentifier(IdGenerator.createIdentifier());
			sms.setPhoneNumber(phoneNumber);
			sms.setContent(content);
			sms.setResult(String.valueOf(result));
//			smsLogDao.insert(sms);
			return result;
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Send sms failure.", e);
			}
			return "nknow";
//			throw new AppBusinessException("Send sms failure.", e);
		}
	}

}
